package com.heu.blood.charge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.charge.vo.BloodChargeHistoryUploadVo;
import com.heu.blood.match.entity.BloodInformationEntity;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.charge.dao.BloodChargeUploadHistoryDao;
import com.heu.blood.charge.entity.BloodChargeUploadHistoryEntity;
import com.heu.blood.charge.service.BloodChargeUploadHistoryService;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("bloodChargeUploadHistoryService")
public class BloodChargeUploadHistoryServiceImpl extends ServiceImpl<BloodChargeUploadHistoryDao, BloodChargeUploadHistoryEntity> implements BloodChargeUploadHistoryService {

    @Override
    public PageUtils queryPage(BloodChargeHistoryUploadVo bloodChargeHistoryUploadVo) {

        LambdaQueryWrapper<BloodChargeUploadHistoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodChargeHistoryUploadVo.getOperatorName()), BloodChargeUploadHistoryEntity::getOperatorName,
                bloodChargeHistoryUploadVo.getOperatorName())
                .eq(Strings.isNotBlank(bloodChargeHistoryUploadVo.getOperatorId()), BloodChargeUploadHistoryEntity::getOperatorId,
                        bloodChargeHistoryUploadVo.getOperatorId())
                .ge(Strings.isNotBlank(bloodChargeHistoryUploadVo.getStartTime()), BloodChargeUploadHistoryEntity::getGmtModified,
                        bloodChargeHistoryUploadVo.getStartTime())
                .le(Strings.isNotBlank(bloodChargeHistoryUploadVo.getEndTime()), BloodChargeUploadHistoryEntity::getGmtModified,
                        bloodChargeHistoryUploadVo.getEndTime())
                .eq(BloodChargeUploadHistoryEntity::getIfShow, IF_SHOW.code());

        Map<String, Object> params = new HashMap<>();
        if (Strings.isNotBlank(bloodChargeHistoryUploadVo.getLimit())){
            params.put("limit", bloodChargeHistoryUploadVo.getLimit());
        }
        if (Strings.isNotBlank(bloodChargeHistoryUploadVo.getPage())){
            params.put("page", bloodChargeHistoryUploadVo.getPage());
        }

        queryWrapper.orderByDesc(BloodChargeUploadHistoryEntity::getGmtModified);
        IPage<BloodChargeUploadHistoryEntity> page = this.page(
                new Query<BloodChargeUploadHistoryEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}